package com.wisdytech.linkdcs.account.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.github.pagehelper.PageInfo;
import com.wisdytech.common.bean.Filter;
import com.wisdytech.common.constants.CommonConstants;
import com.wisdytech.common.exception.ServiceException;
import com.wisdytech.common.handler.BaseService;
import com.wisdytech.common.utils.BaseUtil;
import com.wisdytech.linkdcs.account.dao.TtEqAccountDao;
import com.wisdytech.linkdcs.account.dao.TtEqEquipmentAidDao;
import com.wisdytech.linkdcs.account.model.TtEqAccount;
import com.wisdytech.linkdcs.account.model.TtEqEquipmentAid;
import com.wisdytech.linkdcs.account.service.IAccountRegisterService;
import com.wisdytech.linkdcs.factory.service.IFactoryModelCommonService;
import com.wisdytech.linkdcs.factory.vo.FactoryModelResultVO;
import com.wisdytech.linkdcs.factory.vo.FactoryModelSearchVO;
import com.wisdytech.linkdcs.system.model.SysUser;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("accountRegisterService")
@Transactional
public class AccountRegisterServiceImpl extends BaseService<TtEqAccountDao,TtEqAccount> implements IAccountRegisterService{

    @Resource
    private TtEqAccountDao ttEqAccountDao;
    
    @Resource
    private TtEqEquipmentAidDao ttEEquipmentAidDao;

    @Resource
    private IFactoryModelCommonService factoryModelCommonService;

    @Override
    public PageInfo<Map<String, Object>> listEquipInfoByFilter(String equipCode, String equipName,String operationId,String supplier,Filter filter) {
        Map<String, Object> params = new HashMap<>();
        if(StringUtils.isNotBlank(equipCode)) {
            params.put("equipCode",equipCode);
        }
        if(StringUtils.isNotBlank(equipName)) {
            params.put("equipName",equipName);
        }
        if(StringUtils.isNotBlank(operationId)) {
            params.put("operationId",operationId);
        }
        if(StringUtils.isNotBlank(supplier)) {
            params.put("supplier",supplier);
        }
        List<Map<String,Object>> list = this.beforeList(TtEqAccountDao.class,filter).listEquipInfoByFilter(params);
        for (Map<String, Object> map: list) {
            mergeEquipInfo(map);
        }
        PageInfo<Map<String, Object>> page = new PageInfo<>(list);
        return page;
    }

    @Override
    public Map<String, Object> getEquipInfoByCode(String equipCode) {
        Map<String, Object> equipInfo;
        if(StringUtils.isNotBlank(equipCode)) {
            equipInfo = ttEqAccountDao.getEquipInfoByCode(equipCode);
            mergeEquipInfo(equipInfo);
            return equipInfo;
        }
        return null;
    }

    @Override
    public void accountRegister(TtEqAccount ttEqAccount, SysUser user) {
        //校验操作人
        if(user == null) {
            throw new ServiceException("操作人不能为空");
        }
        if(ttEqAccount!=null) {
        	
        	//删除已存在的辅助类设备
        	EntityWrapper<TtEqEquipmentAid> dew
					=new EntityWrapper<>();
			dew.eq("EQUIP_CODE", ttEqAccount.getEquipCode());
			ttEEquipmentAidDao.delete(dew);
        	
        	//判断是否需要处理辅助类设备关联
        	if(StringUtils.isNoneBlank(ttEqAccount.getAidEquipCodes())) {
        		
        		//添加新的辅助类设备关联
        		String[] aidEquipCodes = ttEqAccount.getAidEquipCodes().split(",");
        		for(String aidEquipCode : aidEquipCodes) {
        			TtEqEquipmentAid aid =new TtEqEquipmentAid();
        			aid.setPkId(BaseUtil.getUUID());
        			aid.setEquipCode(ttEqAccount.getEquipCode());
        			aid.setAidEquipCode(aidEquipCode);
        			ttEEquipmentAidDao.insert(aid);
        		}
        		
        		
        		
        		
        	}
        	
        	
            //修改
            if(StringUtils.isNotBlank(ttEqAccount.getPkId())) {
                ttEqAccount.setUpdateUser(user.getUsername());
                ttEqAccount.setUpdateDate(new Date());
                //ttEqAccountDao.updateById(ttEqAccount);
                ttEqAccountDao.updateEquipInfo(ttEqAccount);
            } else {
            //新增
                ttEqAccount.setPkId(BaseUtil.getUUID());
                ttEqAccount.setDelFlag(CommonConstants.DEL_FLAG);
                ttEqAccount.setCreateUser(user.getUsername());
                ttEqAccount.setCreateDate(new Date());
                ttEqAccountDao.insert(ttEqAccount);
            }

        }
    }

    /**
     * 合并调用接口传入的设备其他信息
     * @param map 传入map
     */
    private void mergeEquipInfo(Map<String, Object> map) {
        if(map!=null) {
            //调通用接口查询设备的厂区，产线，车间，区域信息
            FactoryModelResultVO resultVO = factoryModelCommonService.getFactoryModelResult(new FactoryModelSearchVO(map.get("factoryId")!=null?String.valueOf(map.get("factoryId")):"",
                    map.get("lineId")!=null?String.valueOf(map.get("lineId")):"",
                    map.get("shopId")!=null?String.valueOf(map.get("shopId")):"",
                    map.get("locationId")!=null?String.valueOf(map.get("locationId")):""
            ));
            if(resultVO!=null) {
                map.put("factoryName",resultVO.getFactoryName());
                map.put("lineName",resultVO.getLineName());
                map.put("shopName",resultVO.getShopName());
                map.put("locationName",resultVO.getLocationName());
            }
            
            //补充辅助类设备
            List<TtEqEquipmentAid>  aids =
            		ttEEquipmentAidDao.selectAids(map.get("equipCode").toString());
            map.put("aidEquips", aids);
            
        }

    }

	@Override
	public List<TtEqAccount> searchEquipAcByEquipCode(String search,Filter filter) {
		EntityWrapper<TtEqAccount> ew = new EntityWrapper<>();
		ew.like("EQUIP_CODE", search);
		return this.beforeList(TtEqAccountDao.class,filter).selectList(ew);
	}
}
